## This R code replicates Figures 1, 2, A2, A3, A4, A5, A6, A10, A11, A12 and Table 3

library(cjoint)

## setwd("/Users/sparshasaha/Dropbox/Seeking More Why Women Fail/Pol Behavior Rep Files Ambitious Women 02282020")
## setwd("C:/Users/acw64/Dropbox/Seeking More Why Women Fail/Pol Behavior Rep Files Ambitious Women 02282020")

################################### DLABSS 1 Survey ###################################
#######################################################################################


## DV is vote choice

mydata2 = read.csv("dlabss_one.csv")
dim(mydata2)

## clean up Experience because Excel changed numbers to dates

mydata2$Experience1 <- as.numeric(mydata2$Experience)

## More than 20 = 5 // 10 - 20 =  2 // 5 - 10 = 1 // 5 - 10 = 3 // 0 - 5 = 4

mydata2$Experience2[mydata2$Experience1 == 5] <- "More than 20"
mydata2$Experience2[mydata2$Experience1 == 4] <- "0 - 5"
mydata2$Experience2[mydata2$Experience1 == 3] <- "5 - 10"
mydata2$Experience2[mydata2$Experience1 == 2] <- "10 - 20"
mydata2$Experience2[mydata2$Experience1 == 1] <- "5 - 10"

mydata2$Experience2 <- as.factor(mydata2$Experience2)

## fix levels so they are linear

mydata2$Experience2 <- factor(mydata2$Experience2, levels = c("0 - 5", "5 - 10", "10 - 20", "More than 20"))

mydata2$Experience <- NULL
mydata2$Experience <- mydata2$Experience2

vars<-c('candidate_vote', "Gender", "id", "Progressive",
"Agenda", "Experience", "Personalistic")
newdata2 <- mydata2[,vars]
nrow(newdata2)
head(newdata2)


baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Experience <-"0 - 5"
baselines$Personalistic <-"Empathetic"
baselines$Gender <-"Male"
baselines$Progressive <-"No"

## Figure A10

results2 <- amce(candidate_vote ~ Gender + Experience + 
Personalistic + Agenda + Progressive, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Pr(Candidate Winning)",
ylim=c(-.3,.3), breaks=c(-.5, -.4, -.3, -.2, -.1, 0, .1, .2, .3, .4, .5), labels=c("-.5", "-.4", "-.3", "-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5"), text.size=10)

## DV is vote choice, Governor wording (Figure A2)

mydata3 <- subset(mydata2, group == 1)
nrow(mydata3)

vars<-c('candidate_vote', "Gender", "id", "Progressive",
"Agenda", "Experience", "Personalistic")
newdata2 <- mydata3[,vars]
nrow(newdata2)
head(newdata2)


baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Experience <-"0 - 5"
baselines$Personalistic <-"Empathetic"
baselines$Gender <-"Male"
baselines$Progressive <-"No"


results2 <- amce(candidate_vote ~ Gender + Experience + 
Personalistic + Agenda + Progressive, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Pr(Candidate Winning)",
ylim=c(-.3,.3), breaks=c(-.5, -.4, -.3, -.2, -.1, 0, .1, .2, .3, .4, .5), labels=c("-.5", "-.4", "-.3", "-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5"), text.size=10)

## DV is vote choice, Schoolboard wording (Figure A3)

mydata4 <- subset(mydata2, group == 2)
nrow(mydata4)

vars<-c('candidate_vote', "Gender", "id", "Progressive",
"Agenda", "Experience", "Personalistic")
newdata2 <- mydata4[,vars]
nrow(newdata2)
head(newdata2)


baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Experience <-"0 - 5"
baselines$Personalistic <-"Empathetic"
baselines$Gender <-"Male"
baselines$Progressive <-"No"


results2 <- amce(candidate_vote ~ Gender + Experience + 
Personalistic + Agenda + Progressive, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Pr(Candidate Winning)",
ylim=c(-.3,.3), breaks=c(-.5, -.4, -.3, -.2, -.1, 0, .1, .2, .3, .4, .5), labels=c("-.5", "-.4", "-.3", "-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5"), text.size=10)

## DV is perceived ambitiousness rating, Figure A4

## ONLY keep data in final election (3 and 6) and structure

data <- subset(mydata2, election == 3)

data <-data[order(data$id),]
head(data)
table(data$id)

data$ambition <- ifelse(data$variable=="candidateA", data$Q22_1, data$Q23_1)

data2 <- subset(mydata2, election == 6)

data2 <-data2[order(data2$id),]
head(data2)
table(data2$id)

data2$ambition <- ifelse(data2$variable=="candidateA", data2$Q38_1, data2$Q39_1)

data_final <- rbind(data, data2)

nrow(data_final) ## 1072 candidate level obs ## 535 respondents

## remove NAs in DV

data_final <- data_final[!is.na(data_final$ambition),]

## cjoint analysis next

vars<-c("ambition", "Gender", "id", "Progressive",
"Agenda", "Experience", "Personalistic")
newdata2<- data_final[,vars]
nrow(newdata2)
head(newdata2)

baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Experience <-"0 - 5"
baselines$Personalistic <-"Empathetic"
baselines$Gender <-"Male"
baselines$Progressive <-"No"

results2 <- amce(ambition ~ Gender + Experience + 
Personalistic + Agenda + Progressive, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Ambitiousness Rating by Respondent", ylim=c(-.3,.3), breaks=c(-0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3), labels = c("-0.5", "0", "0.5", "1", "1.5", "2", "2.5", "3"), text.size=13)

################################### DLABSS 2 Survey ###################################
#######################################################################################

## DV is vote choice

mydata2 = read.csv("dlabss_second.csv")
dim(mydata2)

## change Age to factor

mydata2$Age <- as.factor(mydata2$Age)

vars<-c("candidate_vote", "Gender", "id", "Progressive",
"Agenda", "Experience", "Personalistic", "Job", "Age")
newdata2 <- mydata2[,vars]
nrow(newdata2)
head(newdata2)


baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Experience <-"None"
baselines$Personalistic <-"Empathetic"
baselines$Gender <-"Male"
baselines$Progressive <-"No"
baselines$Job <- "Attorney"
baselines$Age <- "35"

# Figure A11

results2 <- amce(candidate_vote ~ Gender + Experience + 
Personalistic + Agenda + Progressive + Job + Age, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Pr(Candidate Winning)",
the_plot <- ylim=c(-.3,.3), breaks=c(-.5, -.4, -.3, -.2, -.1, 0, .1, .2, .3, .4, .5), labels=c("-.5", "-.4", "-.3", "-.2", "-.1", "0", ".1", ".2", ".3", ".4", ".5"), text.size=10)
the_plot


## DV is perceived ambitiousness rating, Figure A5

## ONLY keep data in final election (5) and structure

mydata3 <- mydata2
nrow(mydata3)

newdat <- subset(mydata3, election == 5)
nrow(newdat)
head(newdat)

newdat$Q27_1 ## candidate 1 ambition
newdat$Q29_1 ## candidate 2 ambition

newdat <- newdat[order(newdat$id),]
head(newdat)
table(newdat$id)

newdat$ambition <- ifelse(newdat$variable=="candidateA", newdat$Q27_1, newdat$Q29_1)
head(newdat)

## N = 932 for election 5 only data

## cjoint analysis next

## remove NAs from ambition

newdat <- newdat[!is.na(newdat$ambition),]

vars<-c("ambition", "Gender", "id", "Progressive",
"Agenda", "Experience", "Personalistic", "Job", "Age")
newdata2 <- newdat[,vars]
nrow(newdata2)
head(newdata2)


baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Experience <-"None"
baselines$Personalistic <-"Empathetic"
baselines$Gender <-"Male"
baselines$Progressive <-"No"
baselines$Job <- "Attorney"
baselines$Age <- "35"


results2 <- amce(ambition ~ Gender + Experience + 
Personalistic + Agenda + Progressive + Job + Age, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Ambitiousness Rating by Respondent",
ylim=c(-.3,.3), breaks=c(-3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3), labels = c("-3", "-2.5", "-2", "-1.5", "-1", "-0.5", "0", "0.5", "1", "1.5", "2", "2.5", "3"), text.size=13)

################################### SSI Survey ###################################
#######################################################################################

## DV is vote choice, Figure 2

mydata2 = read.csv("ssi.csv")
dim(mydata2)

vars<-c('candidate_vote', "Gender", "id", "Progressive",
"Agenda", "Parental", "Personalistic")
newdata2<-mydata2[,vars]
nrow(newdata2)
head(newdata2)


baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Parental <-"No children"
baselines$Personalistic <-"Empathetic"
baselines$Gender <-"Male"
baselines$Progressive <-"No"


results2 <- amce(candidate_vote ~ Gender + Parental + 
Personalistic + Agenda + Progressive, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Pr(Candidate Winning)", ylim=c(-.3,.3), breaks=c(-.25, -.2, -.15, -.1, -.05, 0, .05, .1, .15, .2, .25), 
labels=c("-.25", "-.2", "-.15", "-.1", "-.05", "0", ".05", ".1", ".15", ".2", ".25"), text.size=13)

## DV is perceived ambitiousness rating, Figure 1

## ONLY keep data in final election (3) and structure

mydata3 <- mydata2
nrow(mydata3)

newdat <- subset(mydata3, election == 3)
nrow(newdat)
head(newdat)

newdat$Q27_1 ## candidate 1 ambition
newdat$Q29_1 ## candidate 2 ambition

newdat <- newdat[order(newdat$id),]
head(newdat)
table(newdat$id)

newdat$ambition <- ifelse(newdat$variable=="candidateA", newdat$Q27_1, newdat$Q29_1)
head(newdat)
dim(newdat)

## cjoint analysis next

vars<-c("ambition", "Gender", "id", "Progressive",
"Agenda", "Parental", "Personalistic")
newdata2 <- newdat[,vars]
nrow(newdata2)
head(newdata2)

baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Parental <-"No children"
baselines$Personalistic <-"Empathetic"
baselines$Gender <-"Male"
baselines$Progressive <-"No"

results2 <- amce(ambition ~ Gender + Parental + 
Personalistic + Agenda + Progressive, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Ambitiousness Rating by Respondent",
ylim=c(-.3,.3), breaks=c(-3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3), labels = c("-3", "-2.5", "-2", "-1.5", "-1", "-0.5", "0", "0.5", "1", "1.5", "2", "2.5", "3"), text.size=13)

###########################
###AMIE analysis, Table 3 
###########################

library(FindIt)

###Make sure factors are ordered, required by FindIt 
newdata2$Gender<-factor(newdata2$Gender, ordered=FALSE, levels=c("Female", "Male"))
newdata2$Progressive<-factor(newdata2$Progressive, ordered=FALSE, levels=c("No", "Yes"))
newdata2$Personalistic<-factor(newdata2$Personalistic, ordered=TRUE, levels=c("Empathetic", "Good Communicator",
"Hard-Working", "Collaborative", "Assertive", "Tough Negotiator", "Determined to Succeed"))
newdata2$Agenda<-factor(newdata2$Agenda, ordered=TRUE, levels=c("Very Few Changes", "Moderate Changes", 
"Complete Overhaul"))
newdata2$Parental<-factor(newdata2$Parental, ordered=TRUE, levels=c("No children", "1 child", "2 children", "3 children"))

set.seed(1234)
F4<- FindIt(model.treat= ambition ~ Progressive+Personalistic+Agenda+Gender,
             nway=4,
                        data = newdata2,
             type="continuous",
             treat.type="multiple",
             search.lambdas=TRUE)

## Returns coefficient estimates.
summary(F4)

## Returns predicted values for unique treatment combinations.
pred4 <- predict(F4,unique=TRUE)

## Table 3: Top 10 combinations of most ambitious profiles
head(pred4$data, n=10)

## Bottom 10 (not shown)
tail(pred4$data, n=10)

##What happens when we put in parental instead? 

F4<- FindIt(model.treat= ambition ~ Progressive+Gender+Agenda+Personalistic,
             nway=4,
                        data = mydata2,
             type="continuous",
             treat.type="multiple",
             search.lambdas=TRUE)

## Returns coefficient estimates.
summary(F4)

## Returns predicted values for unique treatment combinations.
pred4 <- predict(F4,unique=TRUE)
## Top 10
head(pred4$data, n=10)


################################### Prolific Survey ###################################
#######################################################################################

## DV is vote choice

mydata2 = read.csv("prolific.csv")
dim(mydata2)

vars<-c('candidate_vote', "Gender", "id", "Progressive",
"Agenda", "Parental", "Personalistic")
newdata2<-mydata2[,vars]
nrow(newdata2)
head(newdata2)

## make 'Parental' variable into factor

newdata2$Parental <- as.factor(newdata2$Parental)

baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Parental <- "0"
baselines$Personalistic <- "Empathetic"
baselines$Gender <- "Male"
baselines$Progressive <- "No"

# Figure A12

results2 <- amce(candidate_vote ~ Gender + Parental + 
Personalistic + Agenda + Progressive, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Pr(Candidate Winning)",ylim=c(-.3,.3), breaks=c(-.5, -.25, 0, .25, .5), labels=c("-.5", "-.25", "0", ".25", ".5"), text.size=10)

## DV is perceived ambitiousness rating, Figure A6

## ONLY keep data in final election (5) and structure

mydata3 <- mydata2
nrow(mydata3)

newdat <- subset(mydata3, election == 5)
nrow(newdat)
head(newdat)

newdat$Q27_1 ## candidate 1 ambition
newdat$Q29_1 ## candidate 2 ambition

newdat <- newdat[order(newdat$id),]
head(newdat)
table(newdat$id)

newdat$ambition <- ifelse(newdat$variable=="candidateA", newdat$Q27_1, newdat$Q29_1)
head(newdat)
dim(newdat)

## cjoint analysis next

vars<-c('ambition', "Gender", "id", "Progressive",
"Agenda", "Parental", "Personalistic")
newdata2<- newdat[,vars]
nrow(newdata2)
head(newdata2)

## make 'Parental' variable into factor

newdata2$Parental <- as.factor(newdata2$Parental)

baselines <- list()
baselines$Agenda <- "Very Few Changes"
baselines$Parental <- "0"
baselines$Personalistic <- "Empathetic"
baselines$Gender <- "Male"
baselines$Progressive <- "No"


results2 <- amce(ambition ~ Gender + Parental + 
Personalistic + Agenda + Progressive, data=newdata2,
cluster=TRUE, respondent.id="id", baselines = baselines)
summary(results2)
plot(results2, xlab="Change in Ambitiousness Rating by Respondent",
ylim=c(-.3,.3), breaks=c(-3, -2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5, 3), labels = c("-3", "-2.5", "-2", "-1.5", "-1", "-0.5", "0", "0.5", "1", "1.5", "2", "2.5", "3"), text.size=13)


